package com.tjsinfo.tjsoa.vote.action;

import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.json.JSONArray;
import org.json.JSONObject;

import com.tjsinfo.tjsoa.vote.bo.ITVoteOpinionBoxBO;
import com.tjsinfo.tjsoa.vote.vo.TVoteOpinionBox;

public class OpinionBoxListAction extends DispatchAction {
	private ITVoteOpinionBoxBO opinionBoxBo;
	public void setOpinionBoxBo(ITVoteOpinionBoxBO opinionBoxBo) {
		this.opinionBoxBo = opinionBoxBo;
	}
	
	public ActionForward selectAll(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {	
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		List<TVoteOpinionBox> list = null;
		Integer start = 0;
		JSONArray array = new JSONArray();
		JSONObject object = new JSONObject();
		
		if(request.getParameter("start") == null)
		{
			list = opinionBoxBo.selectAll(0, 10,request);
		}
		else
		{
			start = Integer.parseInt(request.getParameter("start"));
			list = opinionBoxBo.selectAll(start, 10,request);
		}
		
		for (TVoteOpinionBox tvob : list) {
			
			tvob.setDateTime1(sdf.format(tvob.getDateTime()));
			
			switch (tvob.getStatus()) {
			case 0:
				tvob.setStatus1("未查看");
				break;
			case 1:
				tvob.setStatus1("查看");
				break;
			}
			if(tvob.getAnonymity().equals(1)){
				tvob.setAnonymity1("匿名");
			}else{
				tvob.setAnonymity1(tvob.getUserName());
			}
			
			JSONObject giJson = new JSONObject(tvob);
			array.put(giJson);
		}
		
		object.put("totalProperty",opinionBoxBo.selectTotal());
		object.put("root", array);
		response.getWriter().print(object);
		return null;
	}
	
	public ActionForward selectSelf(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {	
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		List<TVoteOpinionBox> list = null;
		Integer start = 0;
		JSONArray array = new JSONArray();
		JSONObject object = new JSONObject();
		
		if(request.getParameter("start") == null)
		{
			list = opinionBoxBo.selectSelf(0, 10,request);
		}
		else
		{
			start = Integer.parseInt(request.getParameter("start"));
			list = opinionBoxBo.selectSelf(start, 10,request);
		}
		
		for (TVoteOpinionBox tvob : list) {
			tvob.setDateTime1(sdf.format(tvob.getDateTime()));
			
			switch (tvob.getStatus()) {
			case 0:
				tvob.setStatus1("未查看");
				break;
			case 1:
				tvob.setStatus1("查看");
				break;
			}
			
			if(tvob.getAnonymity().equals(1)){
				tvob.setAnonymity1("匿名");
			}else{
				tvob.setAnonymity1("自己");
			}
			
			JSONObject giJson = new JSONObject(tvob);
			array.put(giJson);
		}
		
		object.put("totalProperty",opinionBoxBo.selectTotal());
		object.put("root", array);
		response.getWriter().print(object);
		return null;
	}	
}